package api

import (
	"go-weiqi-system/config"
	"go-weiqi-system/internal/models"
	"go-weiqi-system/internal/utils"

	"github.com/gin-gonic/gin"
)

type ActivityController struct{}

// Index 活动列表
func (ctrl *ActivityController) Index(c *gin.Context) {
	var param models.PageParam
	if err := c.ShouldBindQuery(&param); err != nil {
		param.Page = 1
		param.PageSize = 10
	}

	category := c.Query("category")

	var activities []models.Activity
	var total int64

	db := config.DB.Model(&models.Activity{}).Where("status = ?", 1)

	if category != "" {
		db = db.Where("category = ?", category)
	}

	db.Count(&total)
	db.Order("sort desc, id desc").
		Offset(param.GetOffset()).
		Limit(param.GetPageSize()).
		Find(&activities)

	utils.Success(c, models.PageResponse{
		List:     activities,
		Total:    total,
		Page:     param.GetPage(),
		PageSize: param.GetPageSize(),
	})
}

// Detail 活动详情
func (ctrl *ActivityController) Detail(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		utils.Error(c, "参数错误")
		return
	}

	var activity models.Activity
	if err := config.DB.Where("status = ?", 1).First(&activity, id).Error; err != nil {
		utils.Error(c, "活动不存在")
		return
	}

	utils.Success(c, activity)
}

// CategoryList 活动分类列表
func (ctrl *ActivityController) CategoryList(c *gin.Context) {
	categories := []string{"比赛计划", "吃喝玩乐", "社群计划", "其他计划"}
	utils.Success(c, categories)
}
